
const jwt = require("jsonwebtoken")
const guard=(req, res,next)=>{
    // console.log("=============");
    // console.log(req.url);
    // req.session.token={
    //     flag : 3,
    //     username :"超级管理员",
    //     userId: 1
    // }
    // req.session.token=-1;
    // console.log(req.session.token);
    // req.session.token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IjY1YTBlYzM4NWNhNmEwYzFlMjBkMWY4MjcwYzI4MzAzIiwiaW1nIjoiaHR0cHM6Ly9zaG9wd2FsbC0xMzA1ODY5MTA4LmNvcy5hcC1uYW5qaW5nLm15cWNsb3VkLmNvbS9zaG9wd2FsbC82ZTE2NzA2ZDA4Y2Q0ZDZiOTVhMWI2YWExYjBjMjAxMy5qZmlmIiwiZmxhZyI6IjMiLCJzZXgiOiLlpbMiLCJuaWNrbmFtZSI6ImExMjM0NTY1NiIsImJpcnRoIjoiMjAyMS0xLTEiLCJ0ZWxlcGhvbmUiOiIxNzYzNzk0NjMyNSIsImlkIjoiMTkiLCJ2aXAiOiIwIiwiZXhwIjoxNjM4NjA3NjMwLCJlbWFpbCI6IjIzODU4MDQzNTZAcXEuY29tIn0.Cmo6eP0j0j7K4f6AUW4fTU9vquXk2PZXEuUPwjdSaJw'
    // console.log(jwt.decode(req.session.token));
    if((req.session.token==null||!req.session.token) && req.url != '/home/login'){
        res.redirect('/home/login');
    }else{
        if(jwt.decode(req.session.token).flag==3||jwt.decode(req.session.token)==2){
            next();
        }else{
            res.redirect('/home');
        }
        // console.log(jwt.decode(req.session.token));
    }

    // if(req.url !='/admin' && ( req.session.token==null||!req.session.token)){
    //     res.redirect('/admin');
    // }else{
    //     if(req.url == '/conservator/conservatorList' ){
    //         if(req.session.token.flag == 3 ){
    //             next()
    //         }else{
    //             res.redirect('/admin');
    //         }
    //         console.log(req.session.token); 
    //     }else{
    //         next();
    //     }
    // }
}
module.exports=guard;